package com.hqyj.fj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.fj.javaSpringBoot.modules.account.entity.User;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.fj.javaSpringBoot.modules.account.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    /*
     * 127.0.0.1:8081/api/user/insert ---- post
     * {"userName":"fengjin","email":"hj@163.com","password":"123456"}
     * {"name":"hujiang1","email":"hj1@163.com","age":19,"studentCard":{"cardNo":"SFAC_00001"}}
     */
    @PostMapping(value = "/user/insert", consumes = "application/json")
    public Result<User> insertStudent(@RequestBody User user) {

        return userService.editUser(user);
    }

    /*
     *
     * 127.0.0.1:8081/api/user ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /*
     * 127.0.0.1:8081/api/user/2 ---- delete
     */
    @DeleteMapping("/user/{id}")
    @RequiresPermissions("/api/user/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return userService.deleteStudentById(id);
    }
    /*
     * 127.0.0.1:8081/api/user ---- put
     * {"id":1, "name":"hujiangggg","email":"hjjjj@163.com","password":"12345"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updateStudent(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /*
     * 127.0.0.1:8081/api/user ---- put
     * {"id":1, "name":"hujiangggg","email":"hjjjj@163.com","password":"12345"}
     */
    //自己用jpa方法写的接口
    @PutMapping(value = "/user/update", consumes = "application/json")
    public Result<User> updateStudentForMy(@RequestBody User user) {
        return userService.editUser(user);
    }
    /*
     * 127.0.0.1:8081/api/user/1 ---- get
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {

        return userService.getUserById(id);
    }

    /*
     * 127.0.0.1:8081/api/user?userName=hujiang1&email=hj1@163.com ---- get
     */
    @GetMapping("/user")
    public List<User>  getStudentByNameAndEmail(@RequestParam String userName,
                                            @RequestParam String email) {
        return userService.getByUserNameAndEmail(userName, email);
    }

    /*
     * 127.0.0.1:8081/api/user/login?userName=fengjin&password=123456 ---- get
     */
    @GetMapping("/user/login")
    public User getStudentByNameAndPassword(@RequestParam String userName,
                                         @RequestParam String password) {
        return userService.getByUserNameAndPassword(userName, password);
    }

    /*
     *  127.0.0.1:8081/api/users
     *  {"currentPage":1,"pageSize":5,"orderBy":"userName","direction":"desc","keyword":""}
     * */
    @PostMapping(value = "/users",consumes = "application/json")
    public Page <User> getUserBySearchBean(@RequestBody SearchBean searchBean) {
        return   userService.getUserBySearchBean(searchBean);
    }

    /*
     * 127.0.0.1:8081/api/user/v2 ---- post
     * [{"userName":"fengjin2","email":"fj@163.com","password":"123456"},
     * {"userName":"fengjin3","email":"fj1@163.com","password":"123456"}]
     */
    @PostMapping(value = "/user/v2",consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<User> users) {
        return userService.insertUsers(users);
    }
    /*
     * 127.0.0.1:8081/api/login ---- post
     * {"userName":"nrd", "password":"12345"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login(@RequestBody User user) {
        return userService.login(user);
    }

}

